<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\Area;
use app\common\model\User;
use app\common\tool\account\Account;
use app\common\model\Account as AccountModel;
use think\Exception;
use think\facade\Log;

class ReferrerAward
{
    /**
     * 消费区奖励
     * 直推、间推、供应链奖励在确认收货7天后的凌晨进行结算,民宿酒店、本地生活核销后结算
     * 消费-直推/间推奖励
     * @param $order
     * @throws Exception
     */
    public function handle($order)
    {
        try{
            $consume_direct_rate = confValue('consume_direct_rate');
            $consume_indirect_rate = confValue('consume_indirect_rate');
            if($puid = $this->getParentUser($order->uid)){

                $amount = roundNum($order->profit*$consume_direct_rate,2);
                Account::getInstance()->basics($puid, 'love_bean', $amount , '消费区直推奖励', '+')->setOrder($order->id)->createBonus($order->uid,AccountModel::CONSUM_DIRECT);

                if($ppuid = $this->getParentUser($puid)){
                    $amount = roundNum($order->profit*$consume_indirect_rate,2);
                    Account::getInstance()->basics($ppuid, 'love_bean', $amount , '消费区间推奖励', '+')->setOrder($order->id)->createBonus($order->uid,AccountModel::CONSUM_INDIRECT);
                }
            }

            return $this->agent($order);
        }catch (\Exception $e){
            Log::write("listener.ReferrerAward:".$e->getMessage());
            throw new Exception("加载失败：".$e->getMessage(), -1);
        }
    }

    /**
     * 排除普通会员及VIP会员
     * @param $uid
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getParentUser($uid): int
    {
        $user = User::find($uid);
        if(!$parent = User::where('id', $user->parent_id)->find()) return 0;
        if($parent->syn_level > 2) return $parent->id;

        return $this->getParentUser($parent->id);
    }

    private function agent($order)
    {
        $user = User::find($order->uid);
        $agentCityUser = Area::where('code', $user->city)->value('uid');
        if($agentCityUser) {
            $rate = confValue('consume_city_rate');
            $amount = roundNum($order->profit * $rate, 2);
            $account = 'love_bean';
            $note = '消费区市代区域保护奖励';
            Account::getInstance()->basics($agentCityUser, $account, $amount, $note, '+')->setOrder($order->id)->createBonus($user->id, AccountModel::CONSUM_CITY);
        }

        $agentDistrictUser = Area::where('code', $user->district)->value('uid');
        if($agentDistrictUser) {
            $rate = confValue('consume_district_rate');
            $amount = roundNum($order->profit * $rate, 2);
            $account = 'love_bean';
            $note = '消费区区代区域保护奖励';
            Account::getInstance()->basics($agentDistrictUser, $account, $amount, $note, '+')->setOrder($order->id)->createBonus($user->id, AccountModel::CONSUM_CITY);
        }

        return true;
    }
}